home *** CD-ROM | disk | FTP | other *** search
/ Sun Solutions 2000 #2 / Sun Solutions CD (Volume 2 2000)(Special Focus - Java Technologies)(Disc 1).ISO / products / Software / BostonBusinessComputing / bbc / vmail.y < prev    next >
Text File  |  2000-02-11  |  3KB  |  135 lines

  1. #!/bin/sh
  2. test "/$argv[0]" = "/" && exec /bin/sh $0 $argv
  3. (a=1; unset a) 2>/dev/null || {
  4.         [ "`uname -s 2>/dev/null`" = "ULTRIX" ] &&\
  5.     exec /bin/sh5 -$- $0 ${@+"$@"}
  6. }
  7.  
  8. if [ -z "$BBC" ]; then
  9.     echo "$0: BBC environment variable not set.  Can't continue." 1>&2
  10.     exit 1
  11. fi
  12.  
  13. cleanup() {
  14.     status=$?
  15.     [ $status != 0 ] && echo "*** vmail exited with status $status"
  16.     [ -z "$VMVERSIONS" ] && exit 0
  17.     rm="rm -f $vmlocklink"
  18.     [ -n "$vmbackup" ] && cmp $vmfile $vmbackup >/dev/null 2>&1 && rm $vmbackup
  19.     rm="$rm `ls -r $vmfile~* 2>/dev/null | awk '{if (++i > '$VMVERSIONS') print $0}' | sort`"
  20.     exec $rm
  21. }
  22.  
  23. error() {
  24.     echo "$0: $1" 1>&2
  25.     exit 1
  26. }
  27.  
  28. vmdir=$HOME/vmail
  29. vmrc=$vmdir/.vmailrc
  30. id=`(whoami) 2>/dev/null || (id | sed 's/^[^(]*(\([^)]*\)).*/\1/')`
  31. vmfile=$vmdir/MAIL.$id
  32. vmlock=$vmdir/.vmaillock
  33. vmlocklink="$vmlock"-link
  34. host=`(hostname || uname -n) 2>/dev/null`
  35.  
  36. trap : 1
  37. trap "cleanup" 0 1 2 3 4 5 6 7 8 10 12 13 15
  38.  
  39. if [ ! -d $vmdir ]; then
  40.     mkdir $vmdir || error "Couldn't create directory $vmdir"
  41. fi
  42.  
  43. touch $vmlock
  44. if ln $vmlock $vmlocklink >/dev/null 2>&1; then
  45.     echo "    Host=$host
  46.     PID=$$
  47.     Date='`date`'
  48.     TTY=`tty`" > $vmlocklink
  49. else
  50.     . $vmlock
  51.     if [ "$Host" != "$host" ]; then
  52.     state="may be"
  53.     else
  54.     if kill -1 $PID >/dev/null 2>&1; then
  55.         state="is"
  56.     else
  57.         rm -f $vmlocklink
  58.         exec $0 ${@+"$@"}
  59.     fi
  60.     fi
  61.     $BBC/tbput display \
  62. "Another vmail process $state already using the
  63. $vmfile mailbox file.
  64. Here is the information about the other process:
  65.  
  66. `cat $vmlock`
  67.  
  68. You should exit from the other vmail process before proceeding.
  69. File corruption will occur if two processes attempt to modify
  70. the same mail file.  Should I continue? "
  71.     read ans
  72.     case "$ans" in
  73.     y*)    rm -f $vmlock; exec $0 ${@+"$@"}
  74.     esac
  75.     exit 0
  76. fi
  77.  
  78. VMDEFAULT_VERSIONS=5
  79. [ -r $BBC/vmailrc ] && . $BBC/vmailrc
  80.  
  81. [ ! -r $vmrc  -a -n "$VMFORCEDEFAULT" ] && \
  82. echo "VMVERSIONS=$VMDEFAULT_VERSIONS" > $vmrc
  83.  
  84. while [ ! -r $vmrc ]; do
  85.     $BBC/tbput display \
  86. "Vmail has the ability to make backup copies of its saved mail files.
  87. How many versions of the vmail mail files would you like to save? ($VMDEFAULT_VERSIONS) "
  88.     read num || exit 1
  89.     [ -z "$num" ] && num=$VMDEFAULT_VERSIONS
  90.     n="`expr $num + 0 2>/dev/null`"
  91.     if [ -z "$n" ]; then
  92.     echo "Please enter a number."
  93.     continue
  94.     fi
  95.     if [ "$num" -lt 0 ]; then
  96.     echo "Please enter a positive number."
  97.     continue
  98.     fi
  99.     if [ "$num" -eq 0 ]; then
  100.     $BBC/tbput display \
  101. "We recommend that you keep at least one backup copy.
  102. Are you sure that you don't want any backups to be made? "
  103.     read ans
  104.     case "$ans" in
  105.         y*)    ;;
  106.         n*) continue
  107.     esac
  108.     fi
  109.  
  110.     echo "
  111. *** Modifying the file $vmrc.
  112. *** Edit this file in the future if you would like to change the number of
  113. *** mail file backup versions.
  114. "
  115.     echo "VMVERSIONS=$num" > $vmrc || error "Couldn't create file $vmrc"
  116. done
  117.  
  118. . $vmrc
  119. if [ "$VMVERSIONS" -eq 0 ]; then
  120.     vmbackup=''
  121. else
  122.     numver=0
  123.     maxver=0
  124.     for f in $vmfile~*; do
  125.     ver=`expr $f : "$vmdir/MAIL\.$id~\(.*\)$"`
  126.     [ "$ver" = '*' ] && break
  127.     [ "$ver" -gt "$maxver" ] && maxver="$ver"
  128.     numver=`expr $numver + 1`
  129.     done
  130.     vmbackup="$vmfile"`awk 'BEGIN{printf "~%05d", '"$maxver"' + 1}'</dev/null`
  131.     [ -r $vmfile ] && cp $vmfile $vmbackup
  132. fi
  133. trap : 2 3
  134. $BBC/vmail.x ${@+"$@"}
  135.